package cn.keniykey.mc.landterritory.config;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Nullable;

import org.bukkit.Material;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import cn.keniykey.mc.landterritory.LandTerritoryMain;
import cn.keniykey.mc.landterritory.toolkit.Util;

public final class ViewsConfiguration {
	private static final String FILE_NAME = "views.yml";
	private static final int VERSION = 1;
	private static final ViewConfig vc_empty;
	private static HashMap<String,ViewConfig> hashmap_views_config = new HashMap<>();
	
	static {
		vc_empty = new ViewConfig(FILE_NAME, 27, Collections.emptyMap(), Collections.emptyMap());
	}
	
	private static YamlConfiguration getViewsYamlForDir(boolean force) {
		final LandTerritoryMain main = LandTerritoryMain.getPlugin();
		final File dirfile = main.getDataFolder();
		dirfile.mkdirs();
		final File langfile = new File(dirfile, FILE_NAME);
		if(force || !langfile.exists()) {
			if(main.getResource(FILE_NAME) != null) {
				main.saveResource(FILE_NAME, true);
			}else {
				main.getLogger().warning(FILE_NAME + " null");
				return new YamlConfiguration();
			}
		}
		return YamlConfiguration.loadConfiguration(langfile);
	}
	public static void reloadViewsConfiguration() {
		hashmap_views_config.clear();
		YamlConfiguration yaml = getViewsYamlForDir(false);
		if(yaml.getInt("version") != VERSION) {
			LandTerritoryMain.getPlugin().getLogger().warning("配置文件 " + FILE_NAME + " 的version 不是 " + VERSION);
			yaml = getViewsYamlForDir(true);
		}
		final ArrayList<String> view_valuelist = new ArrayList<>(getViewIconKeys(yaml));
		for(final String view_key : yaml.getKeys(false)) {
			if(view_key.equals("version")) continue;
			final String title = yaml.getString(view_key + ".title");
			final int size = yaml.getInt(view_key + ".size");
			final HashMap<String,ItemStack> view_items = new HashMap<>();
			final HashMap<String,Integer> view_item_slot = new HashMap<>();
			for(final String view_icon_key : view_valuelist) {
				if(!view_icon_key.startsWith(view_key)) continue;
				final String icon_name = view_icon_key.substring((view_key.length()+1));
				view_items.put(icon_name, getViewIconItem(yaml, view_icon_key));
				view_item_slot.put(icon_name, yaml.getInt(view_icon_key + ".slot", -1));
			}
			final ViewConfig config_view = new ViewConfig(title, size, Collections.unmodifiableMap(view_items),
					Collections.unmodifiableMap(view_item_slot));
			hashmap_views_config.put(view_key, config_view);
		}
		view_valuelist.clear();
	}
	public static ViewConfig getViewConfig(String view_name) {
		if(hashmap_views_config.containsKey(view_name)) return hashmap_views_config.get(view_name);
		return vc_empty;
	}
	private static HashSet<String> getViewIconKeys(final YamlConfiguration yaml) {
		final HashSet<String> yaml_view_keys = new HashSet<>();
		for(final String key : yaml.getKeys(true)) {
			final String[] array_str = key.split("\\.");
			if(array_str.length > 2) yaml_view_keys.add((array_str[0] + "." + array_str[1]));
		}
		return yaml_view_keys;
	}
	private static ItemStack getViewIconItem(final YamlConfiguration yaml,final String str) {
		final ItemStack item;
		String name = yaml.getString(str + ".item");
		if(name == null) name = "DIRT";
//		short item_endid = 0;
//		if(Pattern.matches("^\\d+$", str)) {
//			item = new ItemStack(Integer.valueOf(str));
//			if(name.contains(":")) {
//				final String[] array_str = name.split(":");
//				try {
//					item_endid = Short.valueOf(array_str[1]);
//				}catch(Exception ex) {}
//			}
//		}else {
//			if(name.contains(":")) {
//				final String[] array_str = name.split(":");
//				name = array_str[0];
//				try {
//					item_endid = Short.valueOf(array_str[1]);
//				}catch(Exception ex) {}
//			}
//			item = new ItemStack(Util.getMaterial(name));
//		}
		item = new ItemStack(Util.getMaterial(name));
//		item.setDurability(item_endid);
		final ItemMeta meta = item.getItemMeta();
		final String disname = yaml.getString(str + ".disname");
		if (disname != null) meta.setDisplayName(Util.conver(disname));
		meta.setLore(Util.conver(yaml.getStringList(str + ".lore")));
		item.setItemMeta(meta);
		return item;
	}
	public static ItemStack setItemMeta(ItemStack item,@Nullable String showname,@Nullable List<String> lore) {
		final ItemMeta meta = item.getItemMeta();
		if(showname != null) meta.setDisplayName(showname);
		if(lore != null && !lore.isEmpty()) meta.setLore(lore);
		item.setItemMeta(meta);
		return item;
	}
	public static final class ViewConfig{
		private final String str_title;
		private final int int_size;
		private final Map<String,ItemStack> map_view_items;
		private final Map<String,Integer> map_view_item_slot;
		private final Map<Integer,String> map_view_slot_name;
		private final Map<Integer,ItemStack> map_view_icons;
		
		ViewConfig(String title, int size,Map<String,ItemStack> view_items,Map<String,Integer> view_item_slot) {
			this.str_title = title;
			this.int_size = size;
			this.map_view_items = view_items;
			this.map_view_item_slot = view_item_slot;
			final HashMap<Integer,ItemStack> view_icons = new HashMap<>();
			final HashMap<Integer,String> view_slot_name = new HashMap<>();
			for(final Entry<String, Integer> entry : view_item_slot.entrySet()) {
				final String icon_name = entry.getKey();
				view_slot_name.put(entry.getValue(), icon_name);
				if(!view_items.containsKey(icon_name)) continue;
				view_icons.put(entry.getValue(), view_items.get(icon_name));
			}
			this.map_view_slot_name = Collections.unmodifiableMap(view_slot_name);
			this.map_view_icons = Collections.unmodifiableMap(view_icons);
		}
		
		public String getViewTitle() {
			return this.str_title;
		}
		public int getViewSize() {
			return this.int_size;
		}
		public Map<String,ItemStack> getViewItems() {
			return this.map_view_items;
		}
		public ItemStack getViewItem(String name) {
			return this.map_view_items.containsKey(name) ? this.map_view_items.get(name).clone() : new ItemStack(Material.STONE);	
		}
		public Map<String,Integer> getViewItemSlot() {
			return this.map_view_item_slot;
		}
		public int getViewItemSlot(String name) {
			return this.map_view_item_slot.containsKey(name) ? this.map_view_item_slot.get(name) : 0;
		}
		public Map<Integer,String> getViewSlotName() {
			return this.map_view_slot_name;
		}
		public String getViewSlotName(int slot) {
			return this.map_view_slot_name.containsKey(slot) ? this.map_view_slot_name.get(slot) : "";
		}
		public Map<Integer,ItemStack> getViewIcons() {
			return this.map_view_icons;
		}
		public ItemStack getViewIcon(int slot) {
			return this.map_view_icons.containsKey(slot) ? this.map_view_icons.get(slot).clone() : new ItemStack(Material.STONE);
		}
	}
}